<?php

class App_Model_DbTable_User_Account_OAuth extends Ext_Db_Table  
{  
	/** Table name */  
    protected $_name = 'user_account_oauth';
    
    private static $_userAccountOAuthRow = array();
    
    public function getRowById($_id)
    {    
        if(!$_id)
        {
            return "";
        }
        
        if(isset(self::$_userAccountOAuthRow['id'][$_id]))
        {
            return self::$_userAccountOAuthRow['id'][$_id];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $_id);
        $row = $this->fetchRow($safe);
        
        if (!$row) {
            //throw new Exception("Count not find row $id");
            return "";
        }
        $_userAccountOAuthRow = $row->toArray();
        
        self::$_userAccountOAuthRow['id'][$_id] = $_userAccountOAuthRow;
        $_identity = $_userAccountOAuthRow['identity'];
        $_site = $_userAccountOAuthRow['site'];
        if($_identity && $_site)
        {
            self::$_userAccountOAuthRow['identity']['site'][$_identity][$_site] = $_userAccountOAuthRow;
        }
        
        return $_userAccountOAuthRow;
    }
    
	public function getRowByIdentitySite($_identity, $_site)
	{
		if(!$_identity || !$_site)
		{
			return "";
		}

		if(isset(self::$_userAccountOAuthRow['identity']['site'][$_identity][$_site]))
		{
			return self::$_userAccountOAuthRow['identity']['site'][$_identity][$_site];
		}
		
		$db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "*")
            ->where('identity = :identity AND site = :site')
            ->limit(1)
            ;
        $params = array('identity' => $_identity, 'site' => $_site);
        $select->bind($params);
        
        $row = $db->fetchRow($select);
        if (!$row) {
            //throw new Exception("Count not find row $id");
            return "";
        }
        $_userAccountOAuthRow = $row;

        self::$_userAccountOAuthRow['identity']['site'][$_identity][$_site] = $_userAccountOAuthRow;
		$_id = $_userAccountOAuthRow['id'];
		if($_id)
		{
		    self::$_userAccountOAuthRow['id'][$_id] = $_userAccountOAuthRow;
		}
		return $_userAccountOAuthRow;
	}
	
	public function addNew($inputArray)
	{
		$sql_data_array = array();
		if(array_key_exists('identity', $inputArray)) $sql_data_array['identity'] = $inputArray['identity'];
		if(array_key_exists('site', $inputArray)) $sql_data_array['site'] = $inputArray['site'];
		if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
		if(!$sql_data_array)
		{
			return "";
		}
		
		try {
			$rows_affected = $this->insert($sql_data_array);
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
	}
	
	public function addOld($inputArray, $id)
	{
		$sql_data_array = array();
		if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
		if(!$sql_data_array)
		{
			return "";
		}
		
		try {
			$db = $this->getAdapter();
			$where = $db->quoteInto(" id = ? ", $id);
			$rows_affected = $this->update($sql_data_array, $where);
			if($rows_affected)
			{
				$this->_unsetUserAccountOAuthRow($id);
			}
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
	}
	
    private function _unsetUserAccountOAuthRow($id)
    {
        if(!$id)
        {
            return "";
        }
        
        $_userAccountOAuthRow = $this->getRowById($id);
        if(!$_userAccountOAuthRow)
        {
            return "";
        }
        
        $_identity = $_userAccountOAuthRow['identity'];
        $_site = $_userAccountOAuthRow['site'];
        
        unset(self::$_userAccountOAuthRow['id'][$id]);
        unset(self::$_userAccountOAuthRow['identity']['site'][$_identity][$_site]);
    }
}  

